package com.enniswu.weitter.widget.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.enniswu.weitter.R;
import com.enniswu.weitter.data.local.DBSupport;
import com.enniswu.weitter.entity.Post;
import com.enniswu.weitter.entity.User;
import com.enniswu.weitter.user.info.UserInfoActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.CommentViewHolder> {

    private List<Post> comments;
    private OnItemClickListener clickListener = new SimpleOnItemClickListener();

    public CommentAdapter(List<Post> comments) {
        this.comments = comments;
    }

    public void setClickListener(PostAdapter.OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @NonNull
    @Override
    public CommentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_comment, parent, false);
        final CommentViewHolder holder = new CommentViewHolder(view);
        view.findViewById(R.id.iv_portrait).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener == null)
                    return;
                int position = holder.getAdapterPosition();
                clickListener.onItemClick(v, position);
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull CommentViewHolder holder, int position) {
        Post post = comments.get(position);
        User user = DBSupport.getInstance().findUser(post.getUserId(), false);
        if (user != null) {
            Glide.with(holder.ivPortrait).load(user.getPortrait())
                    .apply(new RequestOptions().circleCrop()).into(holder.ivPortrait);
            holder.tvName.setText(user.getName());
        }
        holder.tvVariety.setText(post.getCreateTime());
        holder.tvComment.setText(post.getContext());
    }

    @Override
    public int getItemCount() {
        return comments.size();
    }

    static class CommentViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_portrait)
        ImageView ivPortrait;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_variety)
        TextView tvVariety;
        @BindView(R.id.tv_comment)
        TextView tvComment;

        public CommentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class SimpleOnItemClickListener implements OnItemClickListener {
        @Override
        public void onItemClick(View v, int position) {
            Post post = comments.get(position);
            User user = DBSupport.getInstance().findUser(post.getUserId(), false);
            if (user != null) {
                Intent intent = new Intent(v.getContext(), UserInfoActivity.class);
                intent.putExtra("user", user);
                v.getContext().startActivity(intent);
            }

        }
    }
}
